package com.hn.zhijian.base.data.db;

import com.alibaba.druid.pool.DruidDataSource;
import com.hn.zhijian.base.data.common.IDataSource;
import lombok.Data;

import javax.sql.DataSource;


/**
 * @author carl chen
 * @Classname AbstractDataSource
 * @Description TODO
 * @Date 2019/11/20 16:02
 */
@Data

public abstract class AbstractDataSource  implements IDataSource{

    protected String username;

    protected String password;

    protected String url;

    protected String driverClassName;

    protected String dbType;

    protected int initialSize;

    protected Long maxWait;

    protected int maxActive;

    protected Long timeBetweenEvictionRunsMillis;

    protected Long minEvictableIdleTimeMillis;

    protected int minIdle;

    protected String validationQuery;

    protected String testWhileIdle;

    protected boolean testOnBorrow;

    protected boolean testOnReturn;

    protected boolean poolPreparedStatements;

    protected String filters;

    protected int maxPoolPreparedStatementPerConnectionSize;

    protected boolean useGlobalDataSourceStat;

    protected String connectionProperties;

    @Override
    public DataSource dataSurSource()  {
        System.out.println(username);
        DruidDataSource druidDataSource = new DruidDataSource();
        try {
            //数据库用户名
            druidDataSource.setUsername(username);
            //用户密码
            druidDataSource.setPassword(password);
            //连接地址
            druidDataSource.setUrl(url);
            //连接驱动
            druidDataSource.setDriverClassName(driverClassName);
            //数据库类型
            druidDataSource.setDbType(dbType);
            druidDataSource.setInitialSize(initialSize);
            druidDataSource.setMaxWait(maxWait);
            druidDataSource.setMaxActive(maxActive);
            druidDataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
            druidDataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
            druidDataSource.setMinIdle(minIdle);
            druidDataSource.setValidationQuery(validationQuery);
            druidDataSource.setTestOnBorrow(testOnBorrow);
            druidDataSource.setTestOnReturn(testOnReturn);
            druidDataSource.setPoolPreparedStatements(poolPreparedStatements);
            druidDataSource.setFilters(filters);
            druidDataSource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
            druidDataSource.setUseGlobalDataSourceStat(useGlobalDataSourceStat);
            druidDataSource.setConnectionProperties(connectionProperties);
        }catch (Exception e){

        }

        return druidDataSource;
    }


}
